package com.loie.xcx.util;


import com.alibaba.fastjson2.JSONObject;
import com.loie.xcx.config.TaxCloudConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.UUID;

@Component
public class RequestUtil {

    @Autowired
    private TaxCloudConfig taxCloudConfig;



    public   String getRequestData(String url, String type, String body) {

        String appKey = taxCloudConfig.getAppKey();
        String appSecret = taxCloudConfig.getAppSecret();

        String time = String.valueOf(System.currentTimeMillis());
        UUID uuid = UUID.randomUUID();
        String nonce = uuid.toString();
        try {
            body = SecureUtil.encryptAES(body, appSecret);
        }catch(Exception e){
            JSONObject resJson = new JSONObject();
            resJson.put("success", false);
            resJson.put("data", null);
            resJson.put("msg","请求出错");
            return resJson.toJSONString();
        }
        String s = SecureUtil.MD5Encode((time + nonce + appKey + appSecret + body));
        HashMap<String,String> header = new HashMap<>(8,0.75f);
        header.put("Xcx-Timestamp", time);
        header.put("Xcx-Nonce", nonce);
        header.put("Xcx-AppKey", appKey);
        header.put("Xcx-Signature", s);
        if("POST".equalsIgnoreCase(type)){
            return  HttpUtil.httpPostJson(url, header, body, false);


        } else {
            return HttpUtil.httpGet(url, header, false);
        }
    }







}
